using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CapstoneProject.Models;

namespace CapstoneProject.Activities
{
    [Activity(Label = "User Profile")]
    public class ProfileActivity : Activity
    {
        User user;
        ListView list;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Profile);

            user = ((CapstoneApplication)Application).UserRepository.GetAllUsers().FirstOrDefault(u => u.Username == Intent.GetStringExtra("User"));

            TextView usernameHeading = FindViewById<TextView>(Resource.Id.ProfileHeader);
            usernameHeading.Text = user.Username + "'s Profile";

            list = FindViewById<ListView>(Resource.Id.AccountsList);
            list.ItemClick += EditAccount;

            Button editUserButton = FindViewById<Button>(Resource.Id.EditUserButton);

            Button plannerButton = FindViewById<Button>(Resource.Id.PlannerButton);
            plannerButton.Click += delegate { StartActivity(typeof(PlannerActivity)); };

            Button addAccountButton = FindViewById<Button>(Resource.Id.AddButton);
            addAccountButton.Click += AddAccount;
        }

        protected override void OnResume()
        {
            base.OnResume();

            RefreshAccounts();
        }

        private void RefreshAccounts()
        {
            var userAccounts = ((CapstoneApplication)Application).AccountRepository.GetAllAccounts().Where(a => a.UserId == user.UserId);

            double total = 0;
            foreach (Account a in userAccounts)
            {
                UpdateBalance(a);
                total += a.Balance;
            }
            TextView totalLabel = FindViewById<TextView>(Resource.Id.TotalLabel);
            string totalValue = String.Format("Total: {0:C}", total);
            totalLabel.Text = totalValue;
        }

        public void UpdateBalance(Account account)
        {
            BankStatement statement = new BankStatement();
            ((CapstoneApplication)Application).AccountRepository.UpdateAccount(account.AccountId, double.Parse(statement.GetBalance()));
        }

        private void AddAccount(object sender, EventArgs e)
        {
            string username = Intent.GetStringExtra("User");
            Intent i = new Intent();
            i.SetClass(this, typeof(ChooseFIActivity));
            i.AddFlags(ActivityFlags.NewTask);
            i.PutExtra("User", username);
            StartActivity(i);
        }

        private void EditAccount(object sender, ItemEventArgs e)
        {
            Account account = ((AccountListAdapter)list.Adapter).GetAccount(e.Position);
            Intent i = new Intent();
            i.SetClass(this, typeof(EditAccountActivity));
            i.AddFlags(ActivityFlags.NewTask);
            i.PutExtra("Account", account.AccountId);
            i.PutExtra("User", user.Username);
            StartActivity(i);
        }
    }
}